﻿using Dapper;
using Microsoft.EntityFrameworkCore;
using MySql.Data.MySqlClient;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

namespace OracleMysqlDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var conn = new MySqlConnection("server=127.0.0.1;database=wimanu_center;uid=root;pwd=wisekoo2021;charset=utf8;SslMode=None"))
            {
                conn.Open();

                var rows = conn.Query("select * from users limit 10");
                foreach (var row in rows)
                {
                    Console.WriteLine($"{row.id} {row.name}");
                }
            }

            Console.WriteLine("=====================");

            var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            optionsBuilder.UseMySQL("Server=127.0.0.1;database=wimanu_center;uid=root;pwd=wisekoo2021;charset=utf8;SslMode=None");

            using (var DB = new ApplicationDbContext(optionsBuilder.Options))
            {
                var users = DB.Users.AsNoTracking().OrderBy(user => user.Id).Take(10).ToListAsync().Result;
                foreach (var user in users)
                {
                    Console.WriteLine($"{user.Id} {user.Name}");
                }
            }

            Console.ReadKey();
        }
    }

    public class ApplicationDbContext : DbContext
    {
        public DbSet<User> Users { get; set; }

        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
    }

    [Table("users")]
    public class User
    {
        [Column("id")]
        public uint Id { get; set; }
        [Column("name")]
        public string Name { get; set; }
    }
}
